package com.carleasoft.mps.adu.business.dept.service.impl;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carleasoft.mps.adu.business.dept.bean.entity.AuthorityDeptType;
import com.carleasoft.mps.adu.business.dept.bean.entity.DeptEntity;
import com.carleasoft.mps.adu.business.dept.bean.po.AuthorityDeptTypePagePo;
import com.carleasoft.mps.adu.business.dept.bean.po.AuthorityDeptTypePo;
import com.carleasoft.mps.adu.business.dept.dao.AuthorityDeptTypeDao;
import com.carleasoft.mps.adu.business.dept.service.DeptService;
import com.carleasoft.mps.adu.business.dept.service.IAuthorityDeptTypeService;
import com.carleasoft.mps.core.exceptions.BusinessException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

import static com.carleasoft.mps.adu.common.constants.BusinessErrorEnum.DUPLICATE_DEPT_TYPE;
import static com.carleasoft.mps.adu.common.constants.BusinessErrorEnum.DUPLICATE_DEPT_TYPE_NON;

/**
 * <p>
 * 部门分类表 服务实现类
 * </p>
 *
 * @author ztm
 * @since 2021-12-14
 */
@Service
public class AuthorityDeptTypeServiceImpl extends ServiceImpl<AuthorityDeptTypeDao, AuthorityDeptType> implements IAuthorityDeptTypeService {
    @Autowired
    DeptService deptService;

    /**
     * 添加用户类型
     * @param authorityDeptTypePo
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void addDeptType(AuthorityDeptTypePo authorityDeptTypePo) {
        AuthorityDeptType typeEntity = this.getOne(Wrappers.<AuthorityDeptType>lambdaQuery().eq(AuthorityDeptType::getDeptTypeName, authorityDeptTypePo.getDeptTypeName())
                .or().eq(AuthorityDeptType::getDeptTypeNumber,authorityDeptTypePo.getDeptTypeNumber()));
        if(Objects.nonNull(typeEntity)){
            //类型名称重复
            throw new BusinessException(DUPLICATE_DEPT_TYPE);
        }
        //生成Code
        authorityDeptTypePo.setDeptTypeCode(IdWorker.getIdStr());
        //vo转entity
        AuthorityDeptType authorityDeptType = new AuthorityDeptType();
        BeanCopier beanUserCopier = BeanCopier.create(AuthorityDeptTypePo.class, AuthorityDeptType.class, false);
        beanUserCopier.copy(authorityDeptTypePo,authorityDeptType,null);
        this.save(authorityDeptType);
    }

    /**
     * 编辑类型
     * @param authorityDeptTypePo
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void updateDeptType(AuthorityDeptTypePo authorityDeptTypePo) {
        AuthorityDeptType typeEntity = this.getOne(Wrappers.<AuthorityDeptType>lambdaQuery().eq(AuthorityDeptType::getDeptTypeName, authorityDeptTypePo.getDeptTypeName())
                .or().eq(AuthorityDeptType::getDeptTypeNumber,authorityDeptTypePo.getDeptTypeNumber()));
        if(Objects.nonNull(typeEntity) && !authorityDeptTypePo.getDeptTypeCode().equals(typeEntity.getDeptTypeCode())){
            //类型名称重复
            throw new BusinessException(DUPLICATE_DEPT_TYPE);
        }
        //vo转entity
        AuthorityDeptType authorityDeptType = new AuthorityDeptType();
        BeanCopier beanUserCopier = BeanCopier.create(AuthorityDeptTypePo.class, AuthorityDeptType.class, false);
        beanUserCopier.copy(authorityDeptTypePo,authorityDeptType,null);
        this.update(authorityDeptType, Wrappers.<AuthorityDeptType>lambdaUpdate().eq(AuthorityDeptType::getDeptTypeCode,authorityDeptType.getDeptTypeCode()));
    }

    /**
     * 删除部门类型
     * @param deptTypeCode
     * @return void
     */
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void deleteDeptType(String deptTypeCode) {
        //TODO 部门校验
        if(this.deptService.count(Wrappers.<DeptEntity>lambdaQuery().eq(DeptEntity::getDeptTypeCode,deptTypeCode))>0)
            throw new BusinessException(DUPLICATE_DEPT_TYPE_NON);
        this.remove(Wrappers.<AuthorityDeptType>lambdaQuery().eq(AuthorityDeptType::getDeptTypeCode,deptTypeCode));
    }

    @Override
    public List<AuthorityDeptType> deptTypePage(AuthorityDeptTypePagePo authorityDeptTypePagePo) {
        LambdaQueryWrapper<AuthorityDeptType> wrapper = Wrappers.<AuthorityDeptType>lambdaQuery();
        if (StrUtil.isNotBlank(authorityDeptTypePagePo.getDeptTypeNumber())){
            wrapper.like(AuthorityDeptType::getDeptTypeNumber,authorityDeptTypePagePo.getDeptTypeNumber());

        }
        if(StrUtil.isNotBlank(authorityDeptTypePagePo.getDeptTypeName())){
            wrapper.like(AuthorityDeptType::getDeptTypeName,authorityDeptTypePagePo.getDeptTypeName());
        }
        return this.list(wrapper);
    }

    @Override
    public String convertFromCodeToNum(String deptTypeCode){
        return this.getOne(Wrappers.<AuthorityDeptType>lambdaQuery().eq(AuthorityDeptType::getDeptTypeCode,deptTypeCode)).getDeptTypeNumber();
    }

}
